package com.xxl.baselib.baseui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xxl.baselib.R;
import com.xxl.baselib.util.StringUtils;


/**
 * Title: TitleBar.java
 * Description:
 * Copyright (c) 版权所有请勿抄袭 2018
 * Created DateTime: 2018/4/9 17:19
 * Created by xuelianx.
 */
public class TitleBar extends RelativeLayout {
    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private TextView titleText;
    private TextView backTextView;
    private TextView tv_right;

    private void initView() {
        inflate(getContext(), R.layout.layout_titlebar, this);
        titleText = (TextView) findViewById(R.id.title_text);
        backTextView = (TextView) findViewById(R.id.tv_back);
        tv_right = (TextView)findViewById(R.id.tv_right);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setTitle(String title) {
        if (StringUtils.isEmpty(title)) {
            titleText.setText(getResources().getString(R.string.app_name));
        } else {
            titleText.setText(title);
        }
    }

    public void hasBack(OnClickListener onClickListener) {
        backTextView.setVisibility(View.VISIBLE);
        backTextView.setOnClickListener(onClickListener);
    }

    public void hasRight(OnClickListener onClickListener, String str) {
        tv_right.setText(str);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setOnClickListener(onClickListener);
    }
}
